import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

//导入unplugin-vue-components插件
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import ElementPlus from 'unplugin-element-plus/vite'
// https://vite.dev/config/
export default defineConfig({
  //代理服务器
  server:{
    proxy: {
      '/api': {
        target: 'https://apis.juhe.cn', // 目标 API 服务器
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
    },
  },
  //build配置
  build: {
    // 设置警告阈值为 1000 kB
    chunkSizeWarningLimit:1000,
    rollupOptions: {
      output: {
        manualChunks(id) {
          // 将 `node_modules` 中的所有模块分为独立的块
          if (id.includes('node_modules')) {
            return id.split('node_modules/')[1].split('/')[0].toString();
          }
          // 根据路径划分其他模块
          if (id.includes('src/components')) {
            return 'components';
          }
        },
      },
    },
  },
  // ******插件配置******
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      imports:['vue']
    }),
    Components({
      dirs: ['src/components'],
      extensions: ['vue'],
      defaultMatch:'element-plus'
    }),
    ElementPlus({
       // 按需引入的配置
      // 若不需要额外配置，可以留空
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  css: {
    // 全局变量+全局引入scss+配置antdv主题色
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        // 全局变量使用：@primary-color
        additionalData:
          `@use "@/styles/element/index.scss" as *;
          @use "@/styles/element/iconfont.scss" as *;
          @use "@/styles/var.scss" as *;`
      }
    }
  }
})
